# downloader/update_history.py

import os
import json
from datetime import datetime

UPDATE_HISTORY_PATH = os.path.expanduser("~/.video-downloader/update_history.json")

def load_update_history():
    if not os.path.exists(UPDATE_HISTORY_PATH):
        return []
    with open(UPDATE_HISTORY_PATH, "r", encoding="utf-8") as f:
        return json.load(f)

def add_update_record(tool_name, old_version, new_version):
    history = load_update_history()
    record = {
        "tool": tool_name,
        "from": old_version,
        "to": new_version,
        "time": datetime.now().isoformat()
    }
    history.append(record)
    with open(UPDATE_HISTORY_PATH, "w", encoding="utf-8") as f:
        json.dump(history, f, indent=2)

def clear_update_history():
    with open(UPDATE_HISTORY_PATH, "w", encoding="utf-8") as f:
        json.dump([], f)